<?php
if ($vars ['list_user']) {
	$title = elgg_echo ( 'Danh sách đồ án' );
	$content = elgg_view_title ( $title );
	$projectlistview = elgg_list_entities_from_relationship ( array (
			'relationship' => "doneproject",
			'relationship_guid' => elgg_get_logged_in_user_guid (),
			'type' => "object",
			'subtype' => "project",
			'limit' => 70,
			'student_project' => true,
 			'pagination' => true,
			'submitlink' => true 
	) );
	$content .= $projectlistview;
} else {
	$class_state_show = elgg_extract ( 'class_state_show', $vars, STATE_OPEN );
	$title = elgg_echo ( 'submana:listsubpro' );
	$content = elgg_view_title ( $title );
	
	$group = get_entity ( $vars ['group_guid'] );
	elgg_push_breadcrumb ( 'Môn học: ' . $group->name, $group->getURL () );
	elgg_push_breadcrumb ( $title );
	
	$projectlists = elgg_get_entities ( array (
			'type' => "object",
			'subtype' => "projectlist",
			'container_guid' => $vars ['group_guid'] 
	) );
	
	// Show by group project input
	foreach ( $projectlists as $projectlistvalue ) {
		$projectlistvalue->save ();
		$ar_groupproject = explode ( ",", $projectlistvalue->group_project );
		$ar_deadline_editted = explode ( ",", $projectlistvalue->deadline_editted );
		$ar_deadline = explode ( ",", $projectlistvalue->deadline );
		$ar_semester = explode ( ",", $projectlistvalue->semester );
		$ar_academic = explode ( ",", $projectlistvalue->academic_year );
		$classes_states = explode ( ",", $projectlistvalue->classes_state );
		
		$id = 0;
		$count = count ( $ar_groupproject );
		
		// get project by groupproject metadata
		for($index = $count - 1; $index > - 1; $index --) {
			$group_project = $ar_groupproject [$index];
			$semester = $ar_semester [$index];
			$academic_year = $ar_academic [$index];
			$deadline = $ar_deadline [$index];
			$class_state = $classes_states [$index];
			
			// only show project by class state
			if ($class_state_show == STATE_OPEN) {
				if ($class_state != STATE_OPEN) {
					continue;
				}
			} else if ($class_state_show == STATE_CLOSE) {
				if ($class_state != STATE_CLOSE) {
					continue;
				}
			}
			
			if ($class_state == STATE_OPEN || $class_state == STATE_ONGOING) {
				$button_class = "";
			} else {
				$button_class = "elgg-button-cancel";
			}
			if (! empty ( $group_project )) {
				
				// check subject group owner
				if ($group->getOwnerGUID () == elgg_get_logged_in_user_guid ()) {
					
					$group_project_bt = "";
					$group_project_delete = "";
					$group_project_addproject = "";
					$group_project_close = "";
					$del_confirm = elgg_echo ( 'submana:delete' );
					$group_project_delete = elgg_view ( 'output/url', array (
							'id' => "bt_group_project_delete_$id",
							'text' => 'Xóa',
							'rel' => $del_confirm,
							'href' => elgg_add_action_tokens_to_url ( 'submana/deleteprojectlist/' . $group->guid . '/' . $group_project . "/" . $semester . "/" . $academic_year ),
							'class' => 'elgg-button elgg-button-delete elgg-requires-confirmation',
							'style' => "width:8%; float: right; margin-left: 5px;" 
					) );
					if ($class_state != STATE_CLOSE) {
						$group_project_close = elgg_view ( 'output/url', array (
								'id' => "bt_group_project_close_$id",
								'text' => 'Kết thúc',
								'rel' => "Xác nhận kết thúc lớp học?",
								'href' => 'submana/closeclass/' . $group->guid . '/' . $group_project . "/" . $semester . "/" . $academic_year,
								'class' => 'elgg-button elgg-requires-confirmation',
								'style' => "width:15%; float: right;margin-left: 5px;",
								'is_action' => true,
								'is_trusted' => true 
						) );
						
						$group_project_edit = elgg_view ( 'output/url', array (
								'id' => "bt_group_project_edit_$id",
								'text' => 'Sửa',
								'href' => '',
								'class' => 'elgg-button elgg-button-cancel',
								'style' => "width:8%; float: right;margin-left: 5px;" 
						) );
						
						$group_project_addproject = elgg_view ( 'output/url', array (
								'id' => "bt_group_project_add_$id",
								'text' => 'Thêm đề tài',
								'href' => 'submana/addlistpro/' . $group->guid . '/' . $group_project . "/" . $semester . "/" . $academic_year . "/" . "existed",
								'class' => 'elgg-button',
								'style' => "width:20%; float: right;margin-left: 5px;" 
						) );
						$group_project_bt = elgg_view ( 'input/button', array (
								'id' => "bt_group_project_$id",
								'onclick' => "toggle('group_project_$id')",
								'value' => 'Mã lớp: ' . "$group_project" . ", " . $ar_semester [$index] . ", Năm học " . $ar_academic [$index],
								'style' => "width:50%; float:left;",
								'class' => $button_class 
						) );
					} else {
						$group_project_bt = elgg_view ( 'input/button', array (
								'id' => "bt_group_project_$id",
								'onclick' => "toggle('group_project_$id')",
								'value' => 'Mã lớp: ' . "$group_project" . ", " . $ar_semester [$index] . ", Năm học " . $ar_academic [$index],
								'style' => "width:100%; float:left;",
								'class' => $button_class 
						) );
					}
					
					$content .= "<div>$group_project_bt";
					
					$content .= "$group_project_close $group_project_delete $group_project_addproject</div>";
				} else {
					$group_project_bt = elgg_view ( 'input/button', array (
							'id' => "bt_group_project_$id",
							'onclick' => "toggle('group_project_$id')",
							'value' => 'Mã lớp: ' . "$group_project" . ", " . $ar_semester [$index] . ", Năm học " . $ar_academic [$index],
							'style' => "width:100%; float:left;",
							'class' => $button_class 
					) );
					
					$content .= "<div>$group_project_bt</div>";
				}
				
				$content .= "<div class='clearboth'></div>";
				
				// "<div>$group_edit_deadline_all_bt</div>";
				$metadata_options = array (
						array (
								'name' => 'group_project',
								'value' => $group_project 
						),
						array (
								'name' => 'semester',
								'value' => $semester 
						),
						array (
								'name' => 'academic_year',
								'value' => $academic_year 
						) 
				);
				$options = array (
						'type' => "object",
						'subtype' => 'project',
						'deadline_edited' => $ar_deadline_editted [$index],
						'container_guid' => $vars ['group_guid'],
						'hidden_register' => $hidden_register,
						'metadata_name_value_pairs' => $metadata_options 
				);
				
				$projectlist_editpriority = elgg_get_entities_from_metadata ( $options );
				
				foreach ( $projectlist_editpriority as $project_edit ) {
					if (! $project_edit->priority) {
						if ($project_edit->wait_project) {
							$project_edit->priority = PRIORITY_HIGHEST;
						} else if ($project_edit->current_group < $project_edit->max_group) {
							$project_edit->priority = PRIORITY_MEDIUM;
						} else {
							$project_edit->priority = PRIORITY_LOW;
						}
						
						$project_edit->save ();
					}
				}
				
				// hidden register button
				$user = elgg_get_logged_in_user_entity ();
				
				$hidden_register = false;
				if ($user->classes) {
					$classes = explode ( ",", $user->classes );
					for($j = 0; $j < count ( $classes ); $j ++) {
						$class_codes = explode ( "`", $classes [$j] );
						
						if ($class_codes [0] == $group_project && $class_codes [1] == $semester && $class_codes [2] == $academic_year) {
							$hidden_register = true;
						}
					}
				}
				
				$options = array (
						'type' => "object",
						'subtype' => 'project',
						'limit' => 70,
						'deadline_edited' => $ar_deadline_editted [$index],
						'container_guid' => $vars ['group_guid'],
						'hidden_register' => $hidden_register,
						'metadata_name_value_pairs' => $metadata_options,
						'order_by_metadata' => array (
								'name' => "priority",
								'direction' => "ASC",
								'as' => "integer" 
						) 
				);
				
				$projectlistview = elgg_list_entities_from_metadata ( $options );
				
				if ($index == $count - 1) {
					$content .= "<div id='group_project_$id' style='display:none;'>";
				} else {
					$content .= "<div id='group_project_$id' style='display:none;'>";
				}
				if ($group->getOwnerGUID () == elgg_get_logged_in_user_guid ()) {
					$group_project_report = elgg_view ( 'output/url', array (
							'id' => "bt_group_project_report_$id",
							'text' => 'Xem thống kê',
							'href' => 'submana/reportproject/' . $group->guid . '/' . $group_project . "/" . $semester . "/" . $academic_year,
							'class' => 'elgg-button',
							'style' => "float: right; margin-left: 5px;" 
					) );
					$content .= "<div>$group_project_report</div>";
				}
				if ($group->getOwnerGUID () !== elgg_get_logged_in_user_guid () && $user->usertype == "Student") {
					$deadline_date_str = explode ( "/", $deadline );
					$deadlineTime = mktime ( 23, 59, 0, $deadline_date_str [1], $deadline_date_str [0], $deadline_date_str [2] );
					$currentdate = strtotime ( "now" );
					// compare to current date
					if ($currentdate <= $deadlineTime) {
						$register_out_of_list = elgg_view ( 'output/url', array (
								'id' => "bt_register_out_of_list_$id",
								'text' => 'Đăng kí ngoài danh sách',
								'href' => elgg_add_action_tokens_to_url ( 'submana/registeroutlist/' . $group->guid . '/' . $group_project . "/" . $semester . "/" . $academic_year . "/" . $projectlistvalue->getGUID () ),
								'class' => 'elgg-button',
								'style' => "float: right; margin-left: 5px;" 
						) );
					}
					$content .= "<div>$register_out_of_list</div>";
				}
				
				if (strlen ( $projectlistview ) > 70) {
					if ($group->canEdit ( elgg_get_logged_in_user_guid () )) {
						$group_edit_deadline_all_bt = elgg_view ( 'outpu  t/url', array (
								'class' => 'right',
								'href' => 'submana/editprojectlistdeadline/' . $group->guid . '/' . $group_project . "/" . $semester . "/" . $academic_year,
								'text' => "Chỉnh sửa hạn chót đăng ký" 
						) );
						
						$content .= "$group_edit_deadline_all_bt";
					}
				}
				
				if (strlen ( $projectlistview ) <= 70) {
					$content .= '<div><b>Chưa có đề tài nào</b></div>';
				} else {
					if ($ar_deadline_editted [$index] == "0") {
						$content .= "<div><b>Ngày hết hạn đăng ký: $deadline</b></div>";
					}
				}
				$content .= "<div class='clearboth'> </div>";
				$content .= $projectlistview;
				
				$content .= "</div>";
				
				if (strlen ( $projectlistview ) <= 70) {
					$content .= "<hr/>";
				}
				
				$id ++;
			}
		}
		if ($class_state_show != STATE_CLOSE) {
			$content.= "<br>";
			$close_url = "submana/listpro/" . $vars ['group_guid'] . "/close";
			$content .= elgg_view ( 'output/url', array (
					'text' => 'Các lớp đã kết thúc',
					'href' => $close_url 
			) );
			$content.= "<br><hr>";
		}
		break;
	}
}

if (! $projectlistview) {
	$content .= elgg_echo ( 'submana:noneentryprojects' );
}

$sidebar = "";
$params = array (
		'content' => $content,
		'sidebar' => $sidebar 
);
$body = elgg_view_layout ( 'one_sidebar', $params );
// start building the main column of the page

echo elgg_view_page ( $title, $body );

?>


<script type="text/javascript">
    function toggle(project_group) {
        $("#" + project_group).toggle();
    }
</script>
